/*==============================================================================
** Copyright (C) 2024-2027 WingSummer
**
** This program is free software: you can redistribute it and/or modify it under
** the terms of the GNU Affero General Public License as published by the Free
** Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
** FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
** details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program. If not, see <https://www.gnu.org/licenses/>.
** =============================================================================
*/

#include "sharedmemory.h"

#include <QDebug>

SharedMemory::SharedMemory(const QString &key, OpenMode mode, QObject *parent)
    : WingHex::WingIODevice(parent), m_key(key), m_mode(mode),
      m_sharedMemory(key) {}

SharedMemory::~SharedMemory() {
    if (m_sharedMemory.isAttached()) {
        m_sharedMemory.unlock();
        m_sharedMemory.detach();
    }
}

bool SharedMemory::open(OpenMode mode) {
    if ((m_mode == ReadOnly && !(mode & QIODevice::ReadOnly)) ||
        (m_mode == WriteOnly && !(mode & QIODevice::WriteOnly))) {
        return false;
    }

    if (m_mode == ReadOnly && !m_sharedMemory.attach()) {
        qWarning() << "Unable to attach to shared memory segment.";
        return false;
    }

    QSharedMemory::AccessMode m;
    if (mode == ReadOnly) {
        m = QSharedMemory::AccessMode::ReadOnly;
    } else {
        m = QSharedMemory::AccessMode::ReadWrite;
    }

    if (m_sharedMemory.attach(m)) {
        m_sharedMemory.lock(); // Lock memory for reading or writing safely
        return QIODevice::open(mode);
    }

    return false;
}

bool SharedMemory::keepSize() const { return true; }

void SharedMemory::close() {
    m_sharedMemory.unlock();
    m_sharedMemory.detach();
    QIODevice::close();
}

qint64 SharedMemory::readData(char *data, qint64 maxSize) {
    if (m_mode == WriteOnly) {
        return -1; // Writing is not allowed in this mode
    }

    if (maxSize <= 0) {
        return -1;
    }

    auto pos = this->pos();
    const char *sharedMemoryData =
        static_cast<const char *>(m_sharedMemory.constData()) + pos;

    // Copy the data from shared memory
    qint64 dataSize = qMin(maxSize, m_sharedMemory.size() - pos);
    memcpy(data, sharedMemoryData, dataSize);

    return dataSize;
}

qint64 SharedMemory::writeData(const char *data, qint64 maxSize) {
    if (m_mode == ReadOnly) {
        return -1; // Reading is not allowed in this mode
    }

    if (maxSize <= 0) {
        return -1;
    }

    // Copy data to the shared memory
    auto pos = this->pos();
    char *sharedMemoryData = static_cast<char *>(m_sharedMemory.data()) + pos;
    qint64 dataSize = qMin(maxSize, m_sharedMemory.size() - pos);
    memcpy(sharedMemoryData, data, dataSize);

    return maxSize;
}

qint64 SharedMemory::size() const { return m_sharedMemory.size(); }
